package org.dtrd.modules.evaluation.controller;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.evaluation.entity.bean.DiseaseEvaluationInfo;
import org.dtrd.modules.evaluation.entity.dto.DiseaseDTO;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdDiabeticFoot;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdDiabeticFootService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * @Author: QR
 * @Date: 2021/9/28-15:43
 */
@Api(tags = "量表评估")
@RestController
public class DiabeticFootController extends BaseController {

    @Autowired
    private IDtrdEntEvaluationRdDiabeticFootService diabeticFootService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation("添加下肢血管病变及足病评估")
    @PostMapping("/105/24/1052401")
    public Result<?> saveDiabeticFootInfo(@RequestBody DiseaseDTO dto){
        // 参数校验
        if (dto.getIsDiagnose() == null || dto.getSymptom() == null) {
            return Result.error(STATE_PARAMS_MISSING.getCode(), STATE_PARAMS_MISSING.getMsg());
        }
        if (dto.getIsDiagnose() <= 0 || ArrayUtil.hasEmpty((Object) dto.getSymptom())) {
            return Result.error(STATE_PARAMS_ERROR.getCode(), STATE_PARAMS_ERROR.getMsg());
        }
        DiseaseEvaluationInfo info = diabeticFootService.saveDiabeticFootInfo(dto);
        String logContent = String.format("添加下肢血管病变及足病评估，添加用户id：%d", dto.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_ADD);
        return Result.OK(info);
    }

    @ApiOperation("查询最近一次的下肢血管病变及足病评估")
    @GetMapping("/105/24/1052402")
    public Result<?> getDiabeticFootInfoLatest(@RequestParam("patientId") Integer patientId){
        if (isIntParamInvalid(patientId)) {
            return Result.error(STATE_PARAMS_MISSING.getCode(), STATE_PARAMS_MISSING.getMsg());
        }
        DiseaseEvaluationInfo info = diabeticFootService.getDiabeticFootInfo(patientId);
        String logContent = String.format("查询最近一次的下肢血管病变及足病评估，查询用户id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(info);
    }

    @ApiOperation("获取下肢血管病变及足病评估列表")
    @GetMapping("/105/24/1052403")
    public Result<?> getDiabeticFootInfoList(@RequestParam("patientId") Integer patientId){
        if (isIntParamInvalid(patientId)) {
            return error(STATE_PARAMS_MISSING);
        }
        LambdaQueryWrapper<DtrdEntEvaluationRdDiabeticFoot> queryWrapper = Wrappers.lambdaQuery(DtrdEntEvaluationRdDiabeticFoot.class)
                .eq(DtrdEntEvaluationRdDiabeticFoot::getPatientId, patientId);
        List<DtrdEntEvaluationRdDiabeticFoot> poList = diabeticFootService.list(queryWrapper);
        List<DiseaseEvaluationInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()){
            dtoList = poList.stream()
                    .map(po -> new DiseaseEvaluationInfo().parseFromPo(po))
                    .collect(Collectors.toList());
        }
        String logContent = String.format("获取下肢血管病变及足病评估列表，查询用户id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(dtoList);
    }


    @ApiOperation("删除下肢血管病变及足病评估记录")
    @DeleteMapping("/105/24/1052404")
    public Result<?> removeDiabeticFootInfoList(@RequestParam("dataId") Integer dataId){
        if (isIntParamInvalid(dataId)){
            return error(STATE_PARAMS_ERROR);
        }
        boolean result = diabeticFootService.removeById(dataId);
        String logContent = String.format("删除下肢血管病变及足病评估记录，删除记录id：%d", dataId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
